<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- <?php if(!isset($_SESSION)) session_start(); ?> -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Phu Trung Hoa - Control Panel</title>
</head>
<body>	
	<?php 
		/* LIBRARY */
		
		var_dump($_SESSION['uname']);
		
		function dieN($error)
		{
			echo("<div style='color:#FFFFFF'><pre>");
			print_r($error);
			echo("</pre></div>");			
			die();
		}
		/* END LIBRARY */
		
		
		if($_POST["bsubmit"]!="")
		{
			if($_POST["uname"]!=""&&$_POST["pword"]!="")
			{
				try 
				{
					mysql_connect("localhost","root","pth!@#");
					mysql_select_db("db_phutrunghoa");
				}
				catch (Exception $e) 
				{
					dieN($e);
				}
				$sql="SELECT `key`,content FROM tbl_config WHERE `key`='uname' OR `key`='pword'";
				$res=mysql_query($sql) or dieN(mysql_error());
				if(mysql_num_rows($res)!=2)
					dieN("number of rows: ".mysql_num_rows($res)." <> 2");					
				while($row=mysql_fetch_array($res))
				{
					if($row["key"]=="pword")
						$pword=$row["content"];
					else
						$uname=$row["content"];
				}	
				if($_POST["uname"]!=$uname||$_POST["pword"]!=$pword)
				{
					echo 
					"<script>
						alert('Sai username hoặc password. Vui lòng nhập lại!');
					</script>";
				}
				else
				{
					$_SESSION["uname"]=$uname;
					echo 
					"<script>
						/*function goAdmin()
						{
							window.location='admin.php';
						}*/
						alert('Đăng nhập thành công!');
						//setTimeout(goAdmin,1000);
					</script>";
				}
			}
		}
	?>
	
	<script>
	function checkForm()
	{
		if(document.getElementById('uname').value=='')
		{
			alert('Vui lòng nhập username!');
			document.getElementById('uname').focus();
			return false;
		}
		if(document.getElementById('pword').value=='')
		{
			alert('Vui lòng nhập password!');
			document.getElementById('pword').focus();
			return false;
		}
	}
	</script>
	<div style='position: absolute;width: 100%;top:50%;margin-top: -100px;' align='center'>
		<form method="post" action='log.php' name='f' onsubmit="return checkForm()">
			<table style='width:400px'>
				<tr>
					<td width='70px' align="left">Username</td>
					<td><input type='text' name='uname' id='uname' value="<?php echo $_POST["uname"]?>" style='width: 100%'></input></td>
					<td></td>
				</tr>
				<tr>
					<td align="left">Password</td>
					<td><input type="password" name='pword' id='pword' style='width: 100%'></input></td>
					<td align="left">&nbsp;<input type="submit" name='bsubmit' value='Đăng nhập'></input></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>